from aiogram import Bot, types
from aiogram.enums import ChatMemberStatus
from aiogram_dialog import DialogManager
from fluentogram import TranslatorRunner

from poker_bot.config import Settings
from poker_bot.database.services import UserService
from poker_bot.tg_bot.states import CreateDepositSG


async def check_subscription(
    call: types.CallbackQuery, _, manager: DialogManager, **kwargs
):
    bot: Bot = manager.middleware_data.get("bot")
    config: Settings = manager.middleware_data.get("config")
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    user_service: UserService = manager.middleware_data.get("user_service")

    user = await bot.get_chat_member(
        chat_id=config.tg_bot.group_to_subscribe_id, user_id=call.from_user.id
    )
    if user.status in [
        ChatMemberStatus.LEFT,
        ChatMemberStatus.KICKED,
        ChatMemberStatus.RESTRICTED,
    ]:
        await call.answer(
            locale.get(
                "not-subscribed-msg",
            )
        )
        return

    await user_service.update_user_is_subscribed(call.from_user.id)
    await call.answer(locale.get("thanks-for-subscription-msg"))
    await manager.start(CreateDepositSG.select_payment_method)
